package com.huadi.cmcc.service.impl;

import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huadi.cmcc.domain.Admin;
import com.huadi.cmcc.service.AdminService;
import com.huadi.cmcc.mapper.AdminMapper;
import com.huadi.cmcc.utils.MD5Util;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 *
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService{

    @Override
    public Admin login(String username, String password) {
        Wrapper<Admin> query = Wrappers.<Admin>lambdaQuery()
                .eq(Admin::getName, username)
                .last("limit 1");
        final Admin admin = getOne(query);
        if (admin == null) {
            return null;
        }
        boolean same = checkPassword(password, admin.getPassword());
        if (!same) {
            return null;
        }
        return admin;
    }

    /**
     * 检查密码是否正确
     * @param reqPassword
     * @param password
     * @return
     */
    private boolean checkPassword(String reqPassword, String password) {
        final String digest = MD5Util.digest(reqPassword);
        return Objects.equals(password, digest);
    }
}




